
package controle;

import java.io.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import modelo.Categoria;
import persistencia.CategoriaDAO;
import modelo.Subcategoria;
import persistencia.SubcategoriaDAO;
        
        

public class ServletSubcategoria extends HttpServlet {
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String acao = request.getParameter("acao");

        if (acao.equals("salvar")) {//cadastra Subcategoria
            salvar(request, response);
            
        }else if (acao.equals("listar")) {
            getSubcategorias(request, response);

        } else if (acao.equals("excluir")) {
            excluir(request, response);
            
        } 
    } 
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
    

  private void salvar(HttpServletRequest request, HttpServletResponse response) 
           throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        
        String nome_subcategoria = request.getParameter("nome_subcategoria");
        
        String pCat = request.getParameter("categorias");
        pCat = pCat.trim();
        Subcategoria subcategoria = new Subcategoria(nome_subcategoria);
        Categoria cat = CategoriaDAO.getRecuperaCategoria(Integer.parseInt(pCat));
        
        System.out.print(cat);
        
        subcategoria.setCategoria(cat);
        
        String msg = "";
           if (SubcategoriaDAO.salvar(subcategoria)){
                msg = "Subcategoria cadastrada com sucesso";
                 
            }else{
                msg = "Erro ao cadastrar subcategoria";
           }
             request.setAttribute("msg", msg);
             getServletContext().getRequestDispatcher("/CadastrarSubcategorias.jsp").forward(request, response);
    }
  
  protected void getSubcategorias(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        
        try {
            SubcategoriaDAO a = new SubcategoriaDAO();
            Vector subcategoria = a.getSubcategorias();
            request.setAttribute("subcategorias", subcategoria.iterator());
            
            getServletContext().getRequestDispatcher("/ListarExcluirSubcategorias.jsp").forward(request, response);
          }catch(Exception e) {
            System.out.println("Erro no servlet:");
            e.printStackTrace();
            out.close();
        }
    }
    
      protected void excluir(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        int id_subcategoria = Integer.parseInt(request.getParameter("id_subcategoria"));
        SubcategoriaDAO dao = new SubcategoriaDAO();
        dao.excluir(id_subcategoria);
        getServletContext().getRequestDispatcher("/ServletSubcategoria?acao=listar").forward(request, response);
    }  
}
 